package com.qf.factory;

import java.io.IOException;
import java.util.Properties;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.factory
 * @Description:
 * @Date 2022/6/2 10:12
 */
public class MyBeanFactory {
    private Properties properties = new Properties();

    public MyBeanFactory() {
    }

    public MyBeanFactory(String config) throws IOException {
        properties.load(MyBeanFactory.class.getResourceAsStream(config));
    }

    public Object getBean(String beanName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        //1.读取配置文件,获取类的路径, 也就是实现类的权限定名, 例如:com.qf.service.UserServiceImpl1
        String classPath = properties.getProperty(beanName);
        //2.判断这个路径是否为空, 如果不为空再用反射获取类对象
        if (classPath != null){
            //3.如果不为空那么使用反射加载类的对象
            Class clazz = Class.forName(classPath);
            //4.获取对象的实例返回
            return clazz.newInstance();
        }

        //5.如果为空返回null
        return null;
    }
}
